<?php

/**
 * 活动信息表
 */
use Common\Bridge;
use lib\core\AdminAction;

define('SITE_AUTH', 'act-offer-list');
require('../../../common.inc.php');

class actlist extends AdminAction
{
    private $pdo_r;
    private $primary_key = 'Id';
    private $list_url = '/data/modules/act/act.php';

    public function doList()
    {
        $id = (int)$_GET['Id'];
        $uid = (int)$_POST['UserId'];
        $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
        $limit = 20;
        $offset = ($page - 1) * $limit;
        if ($id < 1) {
            HDshowMsg('操作失败！', ['type' => 'error', 'desc' => 'Id为空']);
        }
        $count_sql = "SELECT COUNT(*) AS `num` FROM `ActiveOffer` WHERE ActiveId = $id";
        $where = '';
        if ($uid > 0) {
            $count_sql .= ' and UserId = ' . $uid;
            $where = ' and UserId = ' . $uid;
        }
        $count = $this->readDb($count_sql);
        $count = (int)$count[0]['num'];

        $smarty = $this->app->page();
        $sql = "SELECT * FROM ActiveOffer WHERE ActiveId=$id " . $where . " order by Status DESC, OfferPrice DESC limit $offset, $limit";
        $list = $this->readDb($sql);
        if ($count > 0) {
            if (!empty($data)) {
                $smarty->value('list', $data);
            }

            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;
            $smarty->value('page_info', $page_info);

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('pagination', $pagination);
        }

        $smarty->value('list', $list);
        $smarty->value('id', $id);
        $smarty->value('uid', $uid > 0 ? $uid : '');
        $smarty->output('act/actofferlist.tpl');
    }

    /**
     * 利用SQL语句读取数据库并返回相应数据
     *
     * @param $sql
     * @return mixed
     */
    private function readDb($sql)
    {
        $pdo = $this->pdo_r;
        if (empty($pdo)) {
            $pdo = $this->pdo_r = Bridge::pdo(Bridge::DB_CENTER_W);
        }

        $obj = $pdo->query($sql);

        return $obj->fetchAll();
    }
}

$app->run();